import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Request } from 'express';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request: Request = context.switchToHttp().getRequest();

    // 校验 headers 中是否包含 token
    // 只是简单的判断
    return (
      typeof request.headers.token === 'string' &&
      request.headers.token.trim().length > 0
    );
  }
}
